// src/store/location.ts
import { defineStore } from 'pinia'

interface LocationData {
  latitude: number
  longitude: number
  address: string
}

export const useLocationStore = defineStore('location', {
  state: () => ({
    selectedLocation: null as LocationData | null,
    tempLocation: null as LocationData | null,
  }),

  actions: {
    // 设置临时位置（在地图页面选择时使用）
    setTempLocation(location: LocationData) {
      this.tempLocation = location
    },

    // 确认选择位置
    confirmLocation() {
      if (this.tempLocation) {
        this.selectedLocation = this.tempLocation
        this.tempLocation = null
      }
    },

    // 清除位置数据
    clearLocation() {
      this.selectedLocation = null
      this.tempLocation = null
    },
  },

  // 持久化配置（可选）
  persist: {
    key: 'location-storage',
    paths: ['selectedLocation'], // 只持久化已确认的位置
  },
})
